var {
  randomUtils,
  textUtils,
  timeUtils,
  clickUtils,
  ocrUtils,
  fmtUtils,
  appUtils,
  logUtils,
  pauseUtils,
  clickContent,
} = require("../../utils/index");
var adCommon = require("../adCommon");
module.exports = {
  watchAd(span, handleNextAd) {
    this.appName = "悟空浏览器";
    this.span = span;
    this.handleNextAd = handleNextAd;
    var startTime = Date.now();
    var count = 0;
    var hasCheckDetail = false;
    adCommon.adCount++;
    logUtils.info("第", adCommon.adCount, "/", adCommon.maxAdCount, "个广告");
    while (true) {
      count++;
      var hasExec = timeUtils.hasExec(startTime);
      if (count % 5 == 0) {
        logUtils.info("已经观看", hasExec, "/", this.span);
      }
      if (!hasCheckDetail) {
        if (this.checkDetail()) {
          hasCheckDetail = true;
          this.span = this.span + 20;
        }
      }

      if (count >= 4 && this.isOver()) {
        this.stopAd();
        break;
      }
      if (hasExec > this.span) {
        if (count % 5 != 0) {
          logUtils.info("已经观看", hasExec, "/", this.span, "将执行关闭广告");
        }
        this.stopAd();
        break;
      }
      sleep(2 * 1000);
    }
  },
  stopAd() {
    for (var index = 0; index < 2; index++) {
      back();
      sleep(randomUtils.getRandom(1.5, 2, 1000));
      var exitContents = [
        //prettier-disable
        "坚持退出",
      ];
      var hasNextContents = [
        //prettier-disable
        "看视频再得",
      ];
      if (adCommon.adCount >= adCommon.maxAdCount) {
        var TextUI = textUtils.existsOneTextStartsWith(exitContents);
        if (TextUI) {
          clickUtils.clickTextUI(TextUI);
          sleep(randomUtils.getRandom(1.5, 2, 1000));
          return;
        }
      } else {
        var TextUI = textUtils.existsOneTextStartsWith(hasNextContents);
        if (TextUI) {
          var text$ = TextUI.text();
          var goldNumber = fmtUtils.getNumber(text$);
          logUtils.info("发现广告:", text$, goldNumber);
          if (goldNumber > 1000) {
            clickUtils.clickTextUI(TextUI);
            sleep(randomUtils.getRandom(1.5, 2, 1000));
            this.handleNextAd && this.handleNextAd();
            return;
          }
          if (goldNumber > 500) {
            pauseUtils.pause("广告金币小于1000，需要人工干预");
            pauseUtils.pause("是否已经开始下一个广告？");
            sleep(randomUtils.getRandom(1, 5, 2, 1000));
            this.handleNextAd && this.handleNextAd();
            return;
          }
          var TextUI = textUtils.existsOneTextStartsWith(exitContents);
          if (TextUI) {
            clickUtils.clickTextUI(TextUI);
            sleep(randomUtils.getRandom(1.5, 2, 1000));
            return;
          }
        }
      }
      if (!this.inAd()) {
        logUtils.info("没有广告，退出");
        return;
      }
    }
  },
  inAd() {
    var contents = [
      //prettier-disable
      "广告",
    ];
    var TextUI = textUtils.existsOneTextStartsWith(contents);
    if (TextUI) {
      return TextUI;
    }
    var TextOCR = ocrUtils.captureExistsOneText(contents);
    if (TextOCR) {
      return TextOCR;
    }
    return false;
  },
  isOver() {
    var result = textUtils.existsTextMatches(/\d{1,3}\s/);
    if (result) {
      logUtils.info("没找到**s,判断广告结束了");
      return true;
    } else {
      logUtils.info("找到**s,判断广告正在播放");
      return false;
    }
  },
  checkDetail() {
    var content = "点击视频";
    if (clickContent(content)) {
      sleep(randomUtils.getRandom(12, 15, 1000));
      if (this.checkOpen()) {
        back();
        sleep(randomUtils.getRandom(2, 3, 1000));
      }
      back();
      sleep(randomUtils.getRandom(2, 3, 1000));
      return true;
    }
    if (this.checkOpen()) {
      return true;
    }
    return false;
  },
  checkOpen() {
    var content = "立即打开";
    if (clickContent(content)) {
      sleep(randomUtils.getRandom(3, 5, 1000));
      var content = "允许";
      if (clickContent(content)) {
        sleep(randomUtils.getRandom(3, 5, 1000));
      }
      sleep(randomUtils.getRandom(10, 15, 1000));
      appUtils.open(this.appName);
      return true;
    }
    return false;
  },
};
